package chd.edu.spzx.manager.controller;

import chd.edu.spzx.common.log.annotation.Log;
import chd.edu.spzx.manager.service.SysRoleService;
import chd.edu.spzx.model.dto.system.SysRoleDto;
import chd.edu.spzx.model.entity.system.SysRole;
import chd.edu.spzx.model.vo.common.Result;
import chd.edu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * @author Zhang Chaoliang
 * create 2023
 * 角色控制类
 */
@RestController
@RequestMapping(value = "/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 角色的列表方法
     * @param sysRoleDto 条件角色名称
     * @param current 当前页
     * @param limit 每页显示的记录数
     * @return
     */
    @PostMapping("/findByPage/{current}/{limit}")
    public Result findByPage(@PathVariable(value = "current") Integer current ,
                             @PathVariable(value = "limit") Integer limit,
                             @RequestBody SysRoleDto sysRoleDto) {
        PageInfo<SysRole> pageInfo = sysRoleService.findByPage(sysRoleDto,current,limit);
        return Result.build(pageInfo,ResultCodeEnum.SUCCESS);
    }

    /**
     * 角色添加接口
     * @param SysRole
     * @return
     */
    @Log(title = "角色管理:添加",businessType = 1)
    @PostMapping(value = "/saveSysRole")
    public Result saveSysRole(@RequestBody SysRole SysRole){
        sysRoleService.saveSysRole(SysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 角色修改接口
     * @param sysRole
     * @return
     */
    @PutMapping(value = "/updateSysRole")
    public Result updateSysRole(@RequestBody SysRole sysRole){
        sysRoleService.updateSysRole(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 角色删除接口
     * @param roleId
     * @return
     */
    @DeleteMapping(value = "/deleteById/{roleId}")
    public Result deleteById(@PathVariable(value = "roleId") Long roleId){
        sysRoleService.deleteById(roleId);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询所有的角色
     * @return
     */
    @GetMapping(value = "/findAllRoles/{userId}")
    public Result findAllRoles(@PathVariable(value = "userId") Long userId) {
        Map<String, Object> resultMap = sysRoleService.findAll(userId);
        return Result.build(resultMap , ResultCodeEnum.SUCCESS)  ;
    }
}
